class Solution {
public:
    int minCapability(vector<int>& nums, int k) {
        int l = 1, r = 1e9;
        int ans;
        while (l <= r)
        {
            int mid = (l + r) >> 1;
            if (check(nums, mid) >= k)
            {
                ans = mid;
                r = mid - 1;
            }
            else
                l = mid + 1;
        }
        return ans;
    }

    int check(vector<int>& nums, int ability)
    {
        int cnt = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (ability >= nums[i])
            {
                cnt++;
                i++;
            }
        }
        return cnt;
    }
};